package com.smartaccounting.controller;

import com.smartaccounting.dao.BudgetDAO;
import com.smartaccounting.model.Budget;
import com.smartaccounting.model.User;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.SQLException;

@WebServlet("/budget")
public class BudgetServlet extends HttpServlet {
    private BudgetDAO budgetDAO;

    @Override
    public void init() throws ServletException {
        budgetDAO = new BudgetDAO();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String action = request.getParameter("action");

        try {
            switch (action) {
                case "list":
                    listBudgets(request, response);
                    break;
                case "new":
                    showNewForm(request, response);
                    break;
                case "edit":
                    showEditForm(request, response);
                    break;
                case "delete":
                    deleteBudget(request, response);
                    break;
                default:
                    listBudgets(request, response);
                    break;
            }
        } catch (SQLException e) {
            throw new ServletException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String action = request.getParameter("action");

        try {
            switch (action) {
                case "insert":
                    insertBudget(request, response);
                    break;
                case "update":
                    updateBudget(request, response);
                    break;
            }
        } catch (SQLException e) {
            throw new ServletException(e);
        }
    }

    private void listBudgets(HttpServletRequest request, HttpServletResponse response)
            throws SQLException, ServletException, IOException {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        String monthYear = request.getParameter("monthYear");
        if (monthYear == null) {
            monthYear = java.time.LocalDate.now().getYear() + "-" + String.format("%02d", java.time.LocalDate.now().getMonthValue());
        }

        request.setAttribute("budgets", budgetDAO.getBudgetsByUserIdAndMonth(user.getUserId(), monthYear));
        request.setAttribute("monthYear", monthYear);
        request.getRequestDispatcher("user/budget.jsp").forward(request, response);
    }

    private void showNewForm(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.getRequestDispatcher("user/budget-form.jsp").forward(request, response);
    }

    private void showEditForm(HttpServletRequest request, HttpServletResponse response)
            throws SQLException, ServletException, IOException {
        int budgetId = Integer.parseInt(request.getParameter("budgetId"));
        Budget existingBudget = budgetDAO.getBudgetById(budgetId);
        request.setAttribute("budget", existingBudget);
        request.getRequestDispatcher("user/budget-form.jsp").forward(request, response);
    }

    private void insertBudget(HttpServletRequest request, HttpServletResponse response)
            throws SQLException, IOException {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        String category = request.getParameter("category");
        BigDecimal amount = new BigDecimal(request.getParameter("amount"));
        String monthYear = request.getParameter("monthYear");

        Budget newBudget = new Budget(user.getUserId(), category, amount, monthYear);
        budgetDAO.addBudget(newBudget);
        response.sendRedirect("budget?action=list");
    }

    private void updateBudget(HttpServletRequest request, HttpServletResponse response)
            throws SQLException, IOException {
        int budgetId = Integer.parseInt(request.getParameter("budgetId"));
        BigDecimal amount = new BigDecimal(request.getParameter("amount"));

        Budget budget = new Budget();
        budget.setBudgetId(budgetId);
        budget.setAmount(amount);
        budgetDAO.updateBudget(budget);
        response.sendRedirect("budget?action=list");
    }

    private void deleteBudget(HttpServletRequest request, HttpServletResponse response)
            throws SQLException, IOException {
        int budgetId = Integer.parseInt(request.getParameter("budgetId"));
        budgetDAO.deleteBudget(budgetId);
        response.sendRedirect("budget?action=list");
    }
}